package com.example.haohao.myview;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.haohao.myview.lines.LineGraphs;
import com.example.haohao.myview.lines.Node;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;

public class ShowMyViewActivity extends AppCompatActivity implements SwitchThree.switchClick {

    private MyButton myBtn;
    private SwitchThree switchThree;
    private LineGraphs Lines;
    private ImageView imageView;
    DropDownEditTextView downEditView;
//    branch 修改测试

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_my_view);

        HashMap imgMap= getAssetsImgge();
        imageView = (ImageView) findViewById(R.id.view_img);
        imageView.setImageDrawable(getResources().getDrawable(R.drawable.android_blue));
//        imageView.setImageBitmap(getImageFromAssetsFile(UtilsContext.getAppContext(),"yzzz/Bank_of_China.png"));
        imageView.setImageBitmap((Bitmap) imgMap.get("5000"));
        myBtn = (MyButton) findViewById(R.id.mybtn);
        switchThree = (SwitchThree) findViewById(R.id.ll_switch);
        Lines = (LineGraphs) findViewById(R.id.LineGraphs);

        myBtn.setOnItemSelectListener(new MyButton.OnItemSelectListener() {
            @Override
            public void onItemSelect(String ss) {
                Toast.makeText(ShowMyViewActivity.this,ss,Toast.LENGTH_SHORT).show();
            }
        });

        switchThree.setTitles(new String[]{"当日委托","当日成交","历史",""});
        switchThree.setOnclickListener(this);
        ArrayList<String> list =new ArrayList();
        list.add("one");
        list.add("one");
        list.add("one");
         downEditView=(DropDownEditTextView) findViewById(R.id.downEditView);
        downEditView.setDropDownListData(list,0,true);

        showLineGraghs();
    }

    private void showLineGraghs() {
        ArrayList<Node> nodes = new ArrayList<>();
        nodes.add(new Node(50,200));
        nodes.add(new Node(100,300));
        nodes.add(new Node(150,100));
        nodes.add(new Node(200,200));
        nodes.add(new Node(250,200));
        nodes.add(new Node(300,300));
        nodes.add(new Node(350,100));
        nodes.add(new Node(400,200));
        Lines.setNodeData(nodes);
    }

    private HashMap getAssetsImgge() {

        HashMap<String, Object> map = new HashMap<String, Object>();
        String[] list_image = null;
        Hashtable<String, String> imgBankHash = null;
        try {
            //得到assets/yzzz/目录下的所有文件的文件名，以便后面打开操作时使用
            list_image =getResources().getAssets().list("yzzz");
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        if (list_image.length>0) {
            imgBankHash = new Hashtable<>();
            imgBankHash.put("Industrial_and_Commercial_Bank_of_China.png", "5200");
            imgBankHash.put("China_Everbright_Bank.png", "5800");
            imgBankHash.put("Guangdong_Development_Bank.png", "6200");
            imgBankHash.put("Huaxia_Bank.png", "5900");
            imgBankHash.put("China_Construction_Bank.png", "5000");
            imgBankHash.put("Bank_of_Communications.png", "5400");
            imgBankHash.put("China_Minsheng_Bank.png", "6000");
            imgBankHash.put("Agricultural_Bank_of_China.png", "5100");
            imgBankHash.put("Shanghai_Pudong_Development_Bank.png", "6300");
            imgBankHash.put("China_Citic_Bank.png", "5600");
            imgBankHash.put("Bank_of_Shanghai.png", "7100");
            imgBankHash.put("Industrial_Bank.png", "5700");
            imgBankHash.put("Postal_Savings_Bank_of_China.png", "8500");
            imgBankHash.put("China_Merchants_Bank.png", "5500");
            imgBankHash.put("Bank_of_China.png", "5300");
            imgBankHash.put("Ping_An_Bank.png", "6100");
        }

        InputStream open = null;
        try {
            for (int i = 0; i < list_image.length; ++i) {
                String temp = "yzzz/" + list_image[i];
                open = getAssets().open(temp);
                Bitmap bitmap = BitmapFactory.decodeStream(open);
                map.put(imgBankHash.get(list_image[i]),bitmap);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (open != null) {
                try {
                    open.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return map;
    }
    public static Bitmap getImageFromAssetsFile(Context context, String fileName) {
        Bitmap image = null;
        AssetManager am = context.getResources().getAssets();
        try {
            InputStream is = am.open(fileName);
            image = BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }

    @Override
    public void onSwitch3Title1Clicked() {
        Toast.makeText(this,"1",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onSwitch3Title2Clicked() {
        Toast.makeText(this,"2",Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onSwitch3Title3Clicked() {
        Toast.makeText(this,"3",Toast.LENGTH_SHORT).show();
//        startActivity(new Intent(this,DynamicViewActivity.class));

    }
}
